<!DOCTYPE html>
<html lang="zh-cn">
<head>
   
    <link type="text/css" rel="stylesheet" href="/bundles/blog-common.css?v=KOZafwuaDasEedEenI5aTy8aXH0epbm6VUJ0v3vsT_Q1"/>
<link id="MainCss" type="text/css" rel="stylesheet" href="/skins/ThinkInside/bundle-ThinkInside.css?v=RRjf6pEarGnbXZ86qxNycPfQivwSKWRa4heYLB15rVE1"/>
<link type="text/css" rel="stylesheet" href="/blog/customcss/428549.css?v=%2fam3bBTkW5NBWhBE%2fD0lcyJv5UM%3d"/>

</head>
<body>
<a name="top"></a>

<div id="page_begin_html"></div><script>load_page_begin_html();</script>

<div id="topics">
	<div class = "post">
		<h1 class = "postTitle">
			<a id="cb_post_title_url" class="postTitle2" href="https://www.cnblogs.com/frankdeng/p/9310713.html">Kafka（四）Kafka在zookeeper中的存储</a>
		</h1>
		<div class="clear"></div>
		<div class="postBody">
			<div id="cnblogs_post_body" class="blogpost-body"><h2>一 Kafka在zookeeper中存储结构图</h2>
<p><img src="https://images2018.cnblogs.com/blog/1228818/201805/1228818-20180508101652574-1613892176.png" alt="" width="1000" /></p>
<h2>二 分析</h2>
<h3>2.1 topic注册信息</h3>
<p>/brokers/topics/[topic] :</p>
<p>存储某个topic的partitions所有分配信息</p>
<div class="cnblogs_code">
<pre>[zk: localhost:2181(CONNECTED) 1] get /brokers/topics/firstTopic</pre>
</div>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180805150830809-419872388.png" alt="" /></p>
<div class="cnblogs_code">
<pre>Schema:
{ "version": "版本编号目前固定为数字1", "partitions": {"partitionId编号": [ 同步副本组brokerId列表],"partitionId编号": [同步副本组brokerId列表], .......}}
Example:
{"version": 1,"partitions": {"2": [2, 1, 0],"1": [1, 0, 2],"0": [0, 2, 1],}}</pre>
</div>
<h3>2.2 partition状态信息</h3>
<p>/brokers/topics/[topic]/partitions/[0...N] &nbsp;其中[0..N]表示partition索引号</p>
<p>/brokers/topics/[topic]/partitions/[partitionId]/state</p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180805151156114-465907462.png" alt="" /></p>
<div class="cnblogs_code">
<pre>Schema:
{"controller_epoch": 表示kafka集群中的中央控制器选举次数,"leader": 表示该partition选举leader的brokerId,"version": 版本编号默认为1,
"leader_epoch": 该partition leader选举次数,"isr": [同步副本组brokerId列表]}
Example:
{"controller_epoch":61,"leader":0,"version":1,"leader_epoch":0,"isr":[0,2,1]}</pre>
</div>
<h3>2.3 Broker注册信息</h3>
<p>/brokers/ids/[0...N]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p>
<p class="p1">每个broker的配置文件中都需要指定一个数字类型的id(全局不可重复),此节点为临时znode(EPHEMERAL)</p>
<p class="p1"><img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180805152326448-571172878.png" alt="" /></p>
<div class="cnblogs_code">
<pre>Schema:
{"jmx_port": jmx端口号,"timestamp": kafka broker初始启动时的时间戳,"host": 主机名或ip地址,"version": 版本编号默认为1,
"port": kafka broker的服务端端口号,由server.properties中参数port确定}
Example:
{"jmx_port":-1,"host":"192.168.100.21","timestamp":"1533452008040","port":9092,"version":4}</pre>
</div>
<h3>2.4 Controller epoch</h3>
<p>/controller_epoch --&gt;&nbsp; int (epoch) &nbsp;&nbsp;</p>
<p>此值为一个数字,kafka集群中第一个broker第一次启动时为1，以后只要集群中center controller中央控制器所在broker变更或挂掉，就会重新选举新的center controller，每次center controller变更controller_epoch值就会 + 1;&nbsp;</p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180805152903315-823529001.png" alt="" /></p>
<h3>2.5 Controller注册信息</h3>
<p>/controller -&gt; int (broker id of the controller) &nbsp;存储center controller中央控制器所在kafka broker的信息</p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180805153000339-350296555.png" alt="" /></p>
<div class="cnblogs_code">
<pre>Schema:
{"version": 版本编号默认为1,"brokerid": kafka集群中broker唯一编号,"timestamp": kafka broker中央控制器变更时的时间戳}
Example:
{"version": 1,"brokerid": 0,"timestamp": "1533452008692"}</pre>
</div>
<h3>2.6 Consumer and Consumer group</h3>
<p><img src="https://images2018.cnblogs.com/blog/1228818/201805/1228818-20180508104651193-86573907.png" alt="" width="1000" /></p>
<p>a.每个consumer客户端被创建时,会向zookeeper注册自己的信息;<br />b.此作用主要是为了"负载均衡".<br />c.同一个Consumer Group中的Consumers，Kafka将相应Topic中的每个消息只发送给其中一个Consumer。<br />d.Consumer Group中的每个Consumer读取Topic的一个或多个Partitions，并且是唯一的Consumer；<br />e.一个Consumer group的多个consumer的所有线程依次有序地消费一个topic的所有partitions,如果Consumer group中所有consumer总线程大于partitions数量，则会出现空闲情况;</p>
<p><strong>举例说明：</strong></p>
<p>kafka集群中创建一个topic为report-log &nbsp; 4 partitions 索引编号为0,1,2,3，假如有目前有三个消费者node：注意--&gt;一个consumer中一个消费线程可以消费一个或多个partition.如果每个consumer创建一个consumer thread线程,各个node消费情况如下，node1消费索引编号为0,1分区，node2费索引编号为2,node3费索引编号为3，如果每个consumer创建2个consumer thread线程，各个node消费情况如下(是从consumer node先后启动状态来确定的)，node1消费索引编号为0,1分区；node2费索引编号为2,3；node3为空闲状态。</p>
<p><strong>总结：</strong><br />从以上可知，Consumer Group中各个consumer是根据先后启动的顺序有序消费一个topic的所有partitions的。如果Consumer Group中所有consumer的总线程数大于partitions数量，则可能consumer thread或consumer会出现空闲状态。</p>
<h3>2.7 Consumer均衡算法</h3>
<p>当一个group中,有consumer加入或者离开时,会触发partitions均衡.均衡的最终目的,是提升topic的并发消费能力.<br />1) 假如topic1,具有如下partitions: P0,P1,P2,P3<br />2) 加入group中,有如下consumer: C0,C1<br />3) 首先根据partition索引号对partitions排序: P0,P1,P2,P3<br />4) 根据(consumer.id + '-'+ thread序号)排序: C0,C1<br />5) 计算倍数: M = [P0,P1,P2,P3].size / [C0,C1].size,本例值M=2(向上取整)<br />6) 然后依次分配partitions: C0 = [P0,P1],C1=[P2,P3],即Ci = [P(i * M),P((i + 1) * M -1)]</p>
<h3>2.8 Consumer注册信息</h3>
<p class="p1">每个consumer都有一个唯一的ID(consumerId可以通过配置文件指定,也可以由系统生成),此id用来标记消费者信息.</p>
<p>/consumers/[groupId]/ids/[consumerIdString]</p>
<div class="syntaxhighlighter nogutter java">
<p class="p1">是一个临时的znode,此节点的值为请看consumerIdString产生规则,即表示此consumer目前所消费的topic + partitions列表.</p>
<p class="p1">consumerId产生规则：</p>
<blockquote>
<p class="p1">StringconsumerUuid&nbsp;= null;<br />&nbsp; &nbsp; if(config.consumerId!=null &amp;&amp; config.consumerId)<br />&nbsp; &nbsp; &nbsp; consumerUuid = consumerId;<br />&nbsp; &nbsp; else {<br />&nbsp; &nbsp; &nbsp; String&nbsp;uuid&nbsp;= UUID.randomUUID()<br />&nbsp; &nbsp; &nbsp; consumerUuid = "%s-%d-%s".format(<br />&nbsp; &nbsp; &nbsp; &nbsp; InetAddress.getLocalHost.getHostName, System.currentTimeMillis,<br />&nbsp; &nbsp; &nbsp; &nbsp; uuid.getMostSignificantBits().toHexString.substring(0,8));</p>
<p class="p1">&nbsp; &nbsp; &nbsp;}<br />&nbsp; &nbsp; &nbsp;String&nbsp;consumerIdString&nbsp;= config.groupId + "_" + consumerUuid;</p>






















</blockquote>






















</div>
<div class="cnblogs_code">
<pre>[zk: localhost:2181(CONNECTED) 11] get /consumers/console-consumer-2304/ids/console-consumer-2304_hadoop2-1525747915241-6b48ff32</pre>
</div>
<p><img src="https://images2018.cnblogs.com/blog/1228818/201805/1228818-20180508105321039-416763241.png" alt="" width="700" /></p>
<div class="cnblogs_code">
<pre>Schema:
{"version": 版本编号默认为1,"subscription": { //订阅topic列表"topic名称": consumer中topic消费者线程数},"pattern": "static",
"timestamp": "consumer启动时的时间戳"}
Example:
{"version": 1,"subscription": {"topic2": 1},"pattern": "white_list","timestamp": "1525747915336"}</pre>
</div>
<h3>2.9 Consumer owner</h3>
<p>/consumers/[groupId]/owners/[topic]/[partitionId] -&gt;&nbsp;consumerIdString + threadId索引编号</p>
<p class="p1">a) 首先进行"Consumer Id注册";</p>
<p class="p1">b) 然后在"Consumer id 注册"节点下注册一个watch用来监听当前group中其他consumer的"退出"和"加入";只要此znode path下节点列表变更,都会触发此group下consumer的负载均衡.(比如一个consumer失效,那么其他consumer接管partitions).</p>
<p class="p1">c) 在"Broker id 注册"节点下,注册一个watch用来监听broker的存活情况;如果broker列表变更,将会触发所有的groups下的consumer重新balance.</p>
<p class="p1"><img src="https://images2018.cnblogs.com/blog/1228818/201805/1228818-20180508105839526-553140898.png" alt="" width="700" /></p>
<h3>2.10 Consumer offset</h3>
<p>/consumers/[groupId]/offsets/[topic]/[partitionId] -&gt; long (offset)</p>
<p class="p1">用来跟踪每个consumer目前所消费的partition中最大的offset</p>
<p class="p1">此znode为持久节点,可以看出offset跟group_id有关,以表明当消费者组(consumer group)中一个消费者失效,</p>
<p class="p1">重新触发balance,其他consumer可以继续消费.</p>
<p class="p1"><img src="https://images2018.cnblogs.com/blog/1228818/201805/1228818-20180508110049469-140963000.png" alt="" width="700" /></p>
<h3>2.11 Re-assign partitions</h3>
<p>/admin/reassign_partitions</p>
<div class="cnblogs_code">
<pre>{
   "fields":[
      { "name":"version","type":"int","doc":"version id" },
      { "name":"partitions",
         "type":{"type":"array", "items":{
               "fields":[{"name":"topic","type":"string","doc":"topic of the partition to be reassigned"},
                  {"name":"partition", "type":"int","doc":"the partition to be reassigned"},
                  {"name":"replicas","type":"array","items":"int","doc":"a list of replica ids"}],
            }
            "doc":"an array of partitions to be reassigned to new replicas"
         }
      }
   ]
}
 
Example:
{"version": 1,"partitions": [{"topic": "Foo","partition": 1, "replicas": [0, 1, 3] }] }</pre>
</div>
<h3>2.12 Preferred replication election</h3>
<p>/admin/preferred_replica_election</p>
<div class="cnblogs_code">
<pre>{
   "fields":[
      {
         "name":"version",
         "type":"int",
         "doc":"version id"
      },
      {
         "name":"partitions",
         "type":{
            "type":"array",
            "items":{
               "fields":[
                  {
                     "name":"topic",
                     "type":"string",
                     "doc":"topic of the partition for which preferred replica election should be triggered"
                  },
                  {
                     "name":"partition",
                     "type":"int",
                     "doc":"the partition for which preferred replica election should be triggered"
                  }
               ],
            }
            "doc":"an array of partitions for which preferred replica election should be triggered"
         }
      }
   ]
}
 
例子:
 
{
  "version": 1,
  "partitions":
     [
        {
            "topic": "Foo",
            "partition": 1         
        },
        {
            "topic": "Bar",
            "partition": 0         
        }
     ]            
}</pre>
</div>
<h3>2.13 删除topics</h3>
<p>/admin/delete_topics</p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180805161221496-1637269651.png" alt="" /></p>
<div class="cnblogs_code">
<pre>Schema:
{ "fields":
    [ {"name": "version", "type": "int", "doc": "version id"},
      {"name": "topics",
       "type": { "type": "array", "items": "string", "doc": "an array of topics to be deleted"}
      } ]
}
例子:
{ "version": 1,"topics": ["foo", "bar"]}</pre>
</div>
<h3 class="syntaxhighlighter nogutter java">2.14 Topic配置</h3>
<p>/config/topics/[topic_name]</p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201808/1385722-20180805161358152-1040121422.png" alt="" /></p></div

</body>
</html>
